package uit.mmt02.adapter;

import java.util.List;

import uit.mmt02.R;
import uit.mmt02.doituong.OneMail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

// class la 1 adapter bind du lieu tu list Onemail vao rowlayout hien thi len list
public class myadapter extends ArrayAdapter<OneMail>
{
	//
	static final String ATTACH = "attach";
	static final String VOICE = "voice";
	static final String ENCRYPT = "encrypt";
	//
	final static String KEY_SUB = "SUB";
	final static String KEY_FROM = "FROM";
	final static String KEY_TO = "TO";
	final static String KEY_TIME = "TIME";
	final static String KEY_BODY = "BODY";
	final static String KEY_FAVORITE = "FAVORITE";
	final static String KEY_BUNDLE = "BUNDLE";
	
	
	List<OneMail>list;
	Activity context;
	public myadapter(Activity context, List<OneMail>list) 
	{
		super(context, R.layout.row_duyetmail,list);
		this.context = context;
		this.list = list;
		// TODO Auto-generated constructor stub
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		// TODO Auto-generated method stub
		View v = null;
		if(convertView == null)
		{
			LayoutInflater li = context.getLayoutInflater();
			v = li.inflate(R.layout.row_duyetmail, null);
		}
		else
		{
			v = convertView;
		}
		TextView txtStt = (TextView)v.findViewById(R.id.txtSTT);
		TextView txtFrom = (TextView)v.findViewById(R.id.txtNameFrom);
		TextView txtTime = (TextView)v.findViewById(R.id.txtTime);
		TextView txtSub= (TextView)v.findViewById(R.id.txtSub);
		TextView txtbodySort= (TextView)v.findViewById(R.id.txtbodySort);
//		CheckBox cbMark = (CheckBox)v.findViewById(R.id.cb_danhdau);
//		CheckBox cbSao = (CheckBox)v.findViewById(R.id.cb_sao);
		ImageView img_att = (ImageView)v.findViewById(R.id.image_duyetmail_attach);
		ImageView img_voice = (ImageView)v.findViewById(R.id.image_voice);
		ImageView img_encrypt = (ImageView)v.findViewById(R.id.image_mahoa);

		
		//gan vao
		final OneMail onemail = list.get(position);
		txtStt.setText(String.valueOf(onemail.getId()));
		txtFrom.setText(list.get(position).getFrom());
		txtTime.setText(list.get(position).getDateSend());
		txtSub.setText(list.get(position).getSub());
		txtbodySort.setText(list.get(position).getBody());
		img_att.setEnabled(onemail.isFlagAttach());
		img_encrypt.setEnabled(onemail.isFlagEncrypt());
		img_voice.setEnabled(false);
		
		v.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent I_readmail = new Intent(context, uit.mmt02.activity.readmailActivity.class);
				Bundle bundle = binding(onemail);
//				I_readmail.putExtra(bundle);
				I_readmail.putExtras(bundle);
				context.startActivity(I_readmail);
				Log.i("myadapter", "item click");
			}
		});
		return v;
	}
	
	public Bundle binding(OneMail om)
	{
		Bundle bundle = new Bundle();
		bundle.putString(KEY_SUB,om.getSub());
		bundle.putString(KEY_FROM,om.getFrom());
		bundle.putString(KEY_TO,om.getTo());
		bundle.putString(KEY_TIME,om.getDateSend());
		bundle.putString(KEY_BODY,om.getBody());
		bundle.putBoolean(KEY_FAVORITE, om.isSao());
		return bundle;
	}
	
	
	
	
	

}
